package com.hdoit.apollo.web.billing.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.model.BillingPrint;
import com.hdoit.apollo.logical.billing.service.IBillingPrintService;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.common.enums.billing.PrintType;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.viewmodel.billing.BillingPrintSearch;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年08月01日 10:25:07
 * @comment
 */
@Controller
@RequestMapping(value = "/billing/billingPrint/")
public class BillingPrintController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/billing/billingPrint/";
    }

    @Autowired
    private IBillingPrintService billingPrintService;
    
    @Autowired
    private IBillingService billingService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="billingPrintDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    
    private final String detailedPrint="detailedPrint";//先锋电子
    private final String billingPrintView="billingPrint";
    private final String electricalPreview="electricalPreview";//致悦
    private final String electricalPrint="electricalPrint";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,BillingPrintSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(billingPrintService.queryCountMP(search));
        List<BillingPrint> billingPrints= billingPrintService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("billingPrints", billingPrints);
        model.addAttribute("billingPrintSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String billingPrintDetailed(Model model,String key){
        BillingPrint billingPrint=new BillingPrint();
        if(!StringUtils.isEmpty(key)){
            billingPrint=billingPrintService.queryModel(key);
        }
        model.addAttribute("billingPrint", billingPrint);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,BillingPrint billingPrint,String billingId){
    	Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
        if(StringUtils.isEmpty(billingPrint.getPrintId())){
            billingPrint.setPrintId(CommonUtils.getKeyId());
            billingPrint.setCreateDate(DateUtils.getCurrent());
            billingPrint.setDeleteMark(YesOrNoInteger.no);
            billingPrint.setCreateUserId(getLoginModel().getLoginId());
            billingPrint.setCreateUserName(getLoginModel().getLoginName());
            billingPrint.setShopId(shop.getShopId());
            billingPrint.setModelStatus(ModelStatus.add);
        }else{
            billingPrint.setModifyDate(DateUtils.getCurrent());
            billingPrint.setModifyUserId(getLoginModel().getLoginId());
            billingPrint.setModifyUserName(getLoginModel().getLoginName());
            billingPrint.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(billingPrintService.applyChange(billingPrint)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }
        
        Billing billing=new Billing();
        if(!StringUtils.isEmpty(billingId)){
        	billing =billingService.getBillingByKey(billingId);
        	
        }
        model.addAttribute("billing", billing);
        model.addAttribute("billingPrint", billingPrint);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);
        if(PrintType.electrical.equals(billingPrint.getPrintType())) {
        	return view(electricalPreview);
        }
        if (PrintType.pioneer.equals(billingPrint.getPrintType())) {
        	return view(detailedPrint);
		} 
        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(billingPrintService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = electricalPreview, method = { RequestMethod.GET})
    public String electricalPreview(Model model,String key){
    	Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
        Billing billing=new Billing();
        BillingPrint billingPrint=billingPrintService.getModelByType(PrintType.electrical);
        if(billingPrint==null) {
        	billingPrint=new BillingPrint();
        }

        if(!StringUtils.isEmpty(key)){
        	billing =billingService.getBillingByKey(key);
        	
        }
        model.addAttribute("billingPrint", billingPrint);
        model.addAttribute("billing", billing);
        model.addAttribute("shop", shop);
        return view(electricalPreview);
    }
    
    @RequestMapping(value = electricalPrint, method = { RequestMethod.GET})
    public String electricalPrint(Model model,String key,String describe){
    	Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
    	BillingPrint billingPrint=billingPrintService.getModelByType(PrintType.electrical);
    	
        Billing billing=new Billing();
        int count=0;
        if(!StringUtils.isEmpty(key)){
        	billing =billingService.getBillingByKey(key);
        	if(!StringUtils.isEmpty(billing.getOutStock().getOutStockShows())){
        		count=billing.getOutStock().getOutStockShows().size();
        		
        	}
        	
        }
        model.addAttribute("billingPrint", billingPrint);
        model.addAttribute("billing", billing);
        model.addAttribute("count", count);
        model.addAttribute("shop", shop);
        model.addAttribute("describe", describe);
        return view(electricalPrint);
    }
    
    @RequestMapping(value = detailedPrint, method = { RequestMethod.GET})
    public String detailedPrint(Model model,String key){
    	Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
        Billing billing=new Billing();
        BillingPrint billingPrint=billingPrintService.getModelByType(PrintType.pioneer);
        if(billingPrint==null) {
        	billingPrint=new BillingPrint();
        }

        if(!StringUtils.isEmpty(key)){
        	billing =billingService.getBillingByKey(key);
        }
        model.addAttribute("shop", shop);
        model.addAttribute("billing", billing);
        model.addAttribute("billingPrint", billingPrint);
        return view(detailedPrint);
    }
    
    @RequestMapping(value = billingPrintView, method = { RequestMethod.GET})
    public String billingPrint(Model model,String key,String describe){
        Billing billing=new Billing();
        Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
        BillingPrint billingPrint=billingPrintService.getModelByType(PrintType.pioneer);
        int count=0;
        if(!StringUtils.isEmpty(key)){
        	billing =billingService.getBillingByKey(key);
        	if(!StringUtils.isEmpty(billing.getOutStock().getOutStockShows())){
        		count=billing.getOutStock().getOutStockShows().size();
        		System.err.println(count+"先锋");
        	}
        }
        model.addAttribute("describe", describe);
        model.addAttribute("billing", billing);
        model.addAttribute("count", count);
        model.addAttribute("shop", shop);
        model.addAttribute("billingPrint", billingPrint);
        return view(billingPrintView);
    }
}
